﻿using Modules.System.Dept;
using Modules.System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ManagementApi.Models;
using Modules.Corp;
using Modules.System.Area;

namespace ManagementApi
{
    /// <summary>
    /// 归属地信息局部扩展类
    /// </summary>
    public static partial class AreaExceptions
    {
        /// <summary>
        /// 获取VUE树形结构
        /// </summary>
        /// <param name="areas"></param>
        /// <returns></returns>
        public static IEnumerable<VueTree<int>> AsTrees(this IEnumerable<SysArea> areas)
        {
            return RecurChildTrees(0, areas);
        }

        /// <summary>
        /// 递归子地区地区
        /// </summary>
        /// <param name="pid"></param>
        /// <param name="sysAreas"></param>
        /// <returns></returns>
        private static IEnumerable<VueTree<int>> RecurChildTrees(int pid, IEnumerable<SysArea> sysAreas)
        {
            List<VueTree<int>> entities = new List<VueTree<int>>();
            foreach (var item in sysAreas)
            {
                VueTree<int> tree = new();
                if (item.Pid == pid)
                {
                    tree.Value = item.Id;
                    tree.Label = item.Name;
                    tree.Extend1 = item.Deep;
                    tree.Children = RecurChildTrees(item.Id, sysAreas);
                    if (tree.Children.Count() == 0)
                    {
                        tree.leaf = true;
                    }
                    entities.Add(tree);
                }
            }
            return entities;
        }
    }
}
